package cn.cms.taglib;

import org.apache.commons.lang.StringUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by leon on 2017/2/14.
 */
public class DateFormatTag extends TagSupport {

    private String timestamp;

    private String pattern;

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    @Override
    public int doStartTag() throws JspException {

        try {

            long time = Long.valueOf(timestamp);

            if (StringUtils.isBlank(this.getPattern())){

                pattern = "yyyy-MM-dd HH:mm:ss";
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
            String outDateString = dateFormat.format(new Date(time * 1000L));
            JspWriter jspWriter = this.pageContext.getOut();
            jspWriter.print(outDateString);

        }catch (Exception e){

            e.printStackTrace();
        }
        return super.doStartTag();
    }
}
